/*
 *      mastermind.cpp
 *      
 *      Copyright 2008 Marshall <marshall@marshall-laptop>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      Marshall Conover, November 2008 - Mastermind Program
 */

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;

//Variables 
//----------------------------------------------------------------------------------
int turns; // used to keep track of the number of turns that have passed.
int usercode[4]; // The user's secret code
int compcode[4]; // The computer's secret code
bool GameEnder=0;  //if a user wins, this variable is set to 1. 
int PrevGuessH[100][4]; //This variable will hold the previous guesses of the Human.
int PrevGuessC[100][4]; //This variable will hold the previous guesses of the Computer.

//Functions
//----------------------------------------------------------------------------------
bool GuessCorrect(int guess[])
{ 
   for (int counter=0;counter<4;counter++) //check to see if the code is workable
   {
   		if (guess[counter] >6 or guess[counter]<1) //if their code contains a number that doesn't work
    	{
    		cout << "Oops, that code isn't between 1 and 6! Please try another. "<< endl << endl;
    	    return 1;
    	    counter=4; //makes sure the loop exits, so you don't get multiple error messages.
    	}
    	else 
    	{
    		return 0; //Code works.
		}
	}
return 1; // if the program gets here, something went wrong.
}
//----------------------------------------------------------------------------------
int GuessCheck(bool ManOrComp, int guess[])
{
	int counter=0;  //my counting variable
	int counter2=0;  //my second counting variable
	int pegs[2]={0,0}; //first are the black pegs, second are the white.
	bool used[7]={0,0,0,0,0,0,0}; //this keeps track of what numbers already have pegs; 1 is used, 0 is not used.
	if (ManOrComp==0) //if it's a man, baby
	{
		for (counter=0;counter<4;counter++) //Check for black pegs, and remember which #'s are used.
		{
			if (guess[counter]==compcode[counter]) //if the user's guess is exatcly the comp's code  
			{
				pegs[0]++; // add 1 to the black pegs
				used[guess[counter]]=1; // set the number to used; If guess[counter] is
				                        // equal to 6, then used[6] is now set to 1,
				                        // and when I later check, the program will see
				                        // that 6 has been used, and skip the if.
			}
		} 
	    for (counter = 0; counter < 4; counter++)
	    {
			for (counter2 = 0;counter2 < 4; counter2++)
			{
				if (guess[counter] == compcode[counter2] and used[guess[counter]] == 0) //if the user's guess is somewhere in the comp's code  
				{
					pegs[1]++; // add 1 to white pegs
					used[guess[counter2]]=1; //set number to used
				}
			}
		}
//		for (counter=0; counter<4; counter++) cout << compcode[counter]; // prints the comp's code; used to see if the peg counts are right.
		cout << "You got " << pegs[0] << " black pegs, and " << pegs[1] << " white pegs.";
		if (pegs[0]==4)
		{
			cout << " You win!" << endl;
			GameEnder=1;	 
		}
	}
	return 0; 
}
//----------------------------------------------------------------------------------
bool ManGuess()
{
	int userguess[4];
	int counter=0;
	do
	{
		cout << endl << "What do you think the computer's password is? " ;
		if (PrevGuessH[0][0]>0) //If the user had guessed before, print
	    	                    // all of his guesses.
		{
			cout << "Your previous guesses are: " << endl;
			for (int counter=0; counter<99; counter++) //run through all of the spots guesses could be in.
			{
				if (PrevGuessH[counter][0] > 0 ) //if this spot hold a guess, print it
				{
					cout << "| "<< counter+1 << ": "; //Prints which number guess it was
					for (int counter2=0; counter2<4; counter2++) 
					cout << PrevGuessH[counter][counter2] << ", "; //print each part of the array
				}
			}
		}
		cout << endl;
		for (counter=0; counter<4; counter++) 
		{
		  cin >> userguess[counter]; //gets the user's numbers in order.
		}
	}
	while (GuessCorrect(userguess)==1); // If the user's code doesn't return a 1 
	                                    //(fail) from userguess, continue on.
	for	(int x=0;x<4;x++) PrevGuessH[turns][x]=userguess[x]; //records the user's guess
	GuessCheck(0,userguess); //see how close the user is
	return 0;
}
//----------------------------------------------------------------------------------
bool CompuGuess()
{
	return 0;
}
//----------------------------------------------------------------------------------
int GetCode() // Grab the user's code.
{
	int counter=0;
	do
	{
		cout << "What secret code do you want to use? Enter a number between 1 and 6,"<< endl; 
		cout << "then a space, then another number, like this: 1 4 5 6." << endl << endl;
		for (counter=0;counter<4;counter++) // get the code 
		{	
			cin >> usercode[counter]; // gets all four using counter's changing value
		} 
	} 
	while (GuessCorrect(usercode)==1);// If the user's code doesn't return a 1 
	                                  //(fail) from userguess, continue on.
	cout << "So your code is";
	for (int counter5=0;counter5<4;counter5++) // get the code 
	{	
		cout << " " << usercode[counter5];
	} 
	cout <<". I'll make sure to keep it quiet." << endl << endl;
	return 0;
}
//----------------------------------------------------------------------------------
int NewGame()
{
	GameEnder=0; //reset GameEnder for if the user wants to play a new game
	for	(int x=0;x<100;x++) 
		for (int y=0;y<4;y++) PrevGuessH[x][y]=0; //reset the user's previous guesses
	for (int x=0;x<4;x++) compcode[x]= 1+rand() %6; //create a new computer code
	turns=0;
	return 0;
}
//----------------------------------------------------------------------------------
int main(int argc, char** argv)
{ 
	cout << "MMMM     MMMM      AAA      SSSSSSSS   TTTTTTTTTTTT  EEEEEEEE  RRRRR"<< endl;
	cout << "MM MM   MM MM     AA AA    SSSS            TTT       EE        RR  RR"<< endl;
	cout << "MM  MM MM  MM    AAAAAAA    SSSSSSSS       TTT       EEEEEEEE  RRRRR"<< endl;
	cout << "MM   MMM   MM   AA     AA        SSSS      TTT       EE        RR  RR" <<endl;
	cout << "MM    M    MM  AA       AA  SSSSSSSS       TTT       EEEEEEEE  RR   RR"<< endl;
	cout << endl;
	cout << "           MMMM     MMMM  IIIIIIIIIII  NNNN    NN  DDDDD"<< endl;
	cout << "           MM MM   MM MM      III      NN NN   NN  DD  DDD" << endl;
	cout << "           MM  MM MM  MM      III      NN  NN  NN  DD   DDD"<< endl;
	cout << "           MM   MMM   MM      III      NN   NN NN  DD  DDD" <<endl;
	cout << "           MM    M    MM  IIIIIIIIIII  NN    NNNN  DDDDDD"<< endl;
	cout << endl << "                         By Marshall Conover                    " << endl;
	cout << endl; //ASCII ftw
	cout << "The Rules: " << endl
 	     <<"             You and the computer will take turns, each trying to " << endl
	     << "guess each other's special code, which consists of four numbers " << endl
	     << "between 1 and 6. If you have the correct number in the right spot,  " << endl
	     << "you will get a black peg; a correct number in the wrong order will " << endl
	     << "get you a white peg. So, if you guess '1,1,2,3', and the answer is " << endl
	     << "'3,3,2,1', you will get back two white pegs, and a black peg. " << endl
	     << "The first to guess the other's number wins!" << endl<<endl;
	for (int x=0;x<4;x++) compcode[x]= 1+rand() %6; //create the computer code
	GetCode(); //Get the user's code, which is stored globally
	string play="y"; 
	while (play=="y")
	{	
		while (GameEnder==0)
		{
                        ManGuess();
         		CompuGuess();
			turns++;	
		}
	cout << "Good game! Would you like to play again? (y or n) ";	
	cin >> play;
//	if (play == "y" && GameEnder==1) NewGame();
	}
	return 0;
}
//----------------------------------------------------------------------------------
